package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSON;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;

import javax.annotation.Resource;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-07
 */
@Service
public class NursingLevelServiceImpl implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private static final String NURSING_LEVEL_KEY = "nursing:level";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return nursingLevelMapper.selectNursingLevelById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.insertNursingLevel(nursingLevel);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_LEVEL_KEY);
        return i;

    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int i = nursingLevelMapper.updateNursingLevel(nursingLevel);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        int i = nursingLevelMapper.deleteNursingLevelByIds(ids);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int i = nursingLevelMapper.deleteNursingLevelById(id);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 查询所有的护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {
        //先查询redis缓存
        Map<Object, Object> levels = stringRedisTemplate.opsForHash().entries(NURSING_LEVEL_KEY);
        if (!levels.isEmpty()) {
            //缓存命中
            List<NursingLevel> nursingLevels = new ArrayList<>();
            levels.forEach((k, v) -> {
                //json字符串转为对象
                NursingLevel nursingLevel = JSON.parseObject(v.toString(), NursingLevel.class);
                nursingLevels.add(nursingLevel);
            });
            return nursingLevels;
        }
        //查询数据库
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        //处理数据
        LinkedHashMap<Object, Object> nursingLevelMap = new LinkedHashMap<>();
        nursingLevels.forEach(level -> {
            //level转为json字符串
            String json = JSON.toJSONString(level);
            nursingLevelMap.put(level.getId().toString(), json);
        });
        //缓存到redis
        stringRedisTemplate.opsForHash().putAll(NURSING_LEVEL_KEY, nursingLevelMap);
        stringRedisTemplate.expire(NURSING_LEVEL_KEY, 1, TimeUnit.DAYS);
        //返回
        return nursingLevels;
    }
}
